package com.code.leetcode.before50;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/22 20:27
 */
public class The46 {

    public void backtrack(int n, List<Integer> output, List<List<Integer>> lists, int index) {
        if (n == index) {
            lists.add(new ArrayList<>(output));
        }
        for (int i = index; i < n; i++) {
            Collections.swap(output, index, i);
            backtrack(n, output, lists, index + 1);
            Collections.swap(output, index , i);
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        List<Integer> output = new ArrayList<>();
        for (int num : nums) {
            output.add(num);
        }
        backtrack(nums.length, output, lists, 0);
        return lists;
    }
}
